from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import datetime
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import Keys
# 导入其中的webdriver来驱动浏览器
url = "https://kyfw.12306.cn/otn/view/index.html"
login_url = 'https://kyfw.12306.cn/otn/resources/login.html'
order_url = 'https://kyfw.12306.cn/otn/confirmPassenger/initDc'
prefill_url = 'https://kyfw.12306.cn/otn/view/prefill_order.html'

a = '泉州'
b = '北京'
train_num = 'G322'
leave_month = 11
leave_day = 7
now_month = datetime.date.today().month

# 启动Edge浏览器
driver = webdriver.Edge()
# 在地址栏输入网址
driver.get(login_url)
driver.maximize_window()    # 将浏览器窗口最大化
driver.find_element(By.XPATH, '//*[@id="toolbar_Div"]/div[2]/div[2]/ul/li[2]/a').click()
WebDriverWait(driver, 1000).until(EC.url_to_be(url))
time.sleep(1.1)
# 输入出发城市
driver.find_element(By.XPATH, '//*[@id="J-index"]/a').click()
driver.find_element(By.XPATH, '//input[@id="fromStationText"]').click()
time.sleep(2)
driver.find_element(By.XPATH, '//input[@id="fromStationText"]').send_keys(a)
driver.find_element(By.ID, "fromStationText").send_keys(Keys.ENTER)
# 输入到达城市
driver.find_element(By.XPATH, '//input[@id="toStationText"]').click()
time.sleep(2)
driver.find_element(By.XPATH, '//input[@id="toStationText"]').send_keys(b)
driver.find_element(By.ID, "toStationText").send_keys(Keys.ENTER)
time.sleep(2.1)
# 输入出发时间
driver.find_element(By.ID, "train_date").click()
time.sleep(1)
if leave_month == now_month:
    xpath_str = f'//*[@id="toolbar_Div"]/div[10]/div[1]/div[2]/div[{leave_day}]'
    WebDriverWait(driver, 10, 0.1).until(EC.element_to_be_clickable((By.XPATH, xpath_str)))
    driver.find_element(By.XPATH, xpath_str).click()

elif leave_month == now_month + 1:
    xpath_str = f'//*[@id="toolbar_Div"]/div[10]/div[2]/div[2]/div[{leave_day}]'
    WebDriverWait(driver, 10, 0.1).until(EC.element_to_be_clickable((By.XPATH, xpath_str)))
    driver.find_element(By.XPATH, xpath_str).click()

else:
    print("都不是这个月跟下一个月，你买der啊")
time.sleep(2.1)
# 查询
WebDriverWait(driver, 1000, 0.5).until(EC.element_to_be_clickable((By.XPATH, '//a[@id="search_one"]')))
driver.find_element(By.XPATH, '//a[@id="search_one"]').click()
# 切换页面
driver.switch_to.window(driver.window_handles[-1])

WebDriverWait(driver, 10, 0.1).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="preFilled1href"]')))
driver.find_element(By.XPATH, '//*[@id="preFilled1href"]').click()

WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="trainList"]/div')))
train_list = driver.find_elements(By.XPATH, '//*[@id="trainList"]/div')
for item in train_list:
    WebDriverWait(driver,10).until(EC.presence_of_element_located((By.CLASS_NAME, 'ticket-number-num')))
    train_number = item.find_element(By.XPATH, './div[1]/div[1]/div[1]').text
    if train_num == train_number:
        driver.find_element(By.XPATH, '//*[@id="trainList"]/div[2]/div[2]/div/div[5]/div[4]/a').click()


WebDriverWait(driver, 10, 0.1).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="toolbar_Div"]/div[2]/div[3]/div[3]/div[2]/a[2]')))
driver.find_element(By.XPATH, '//*[@id="toolbar_Div"]/div[2]/div[3]/div[3]/div[2]/a[2]').click()


# 设置目标时间
target_hour, target_minute = 17, 0
# 循环等待直到目标时间
while True:
    current_hour, current_minute = time.localtime()[3:5]
    if current_hour == target_hour and current_minute == target_minute:
        break
    # 休眠0.1秒以避免频繁检查时间
    time.sleep(0.1)

driver.refresh()


def click_prefill():
    # 点击“按预填信息购票”
    WebDriverWait(driver, 10, 0.1).until(EC.element_to_be_clickable(
        (By.XPATH, '//div[@class="ticket-result-bd"]/div[1]/div[2]/div/div[1]/div[4]/a'))
    )
    return driver.find_element(By.XPATH, '//div[@class="ticket-result-bd"]/div[1]/div[2]/div/div[3]/div[4]/a')


check_prefill = click_prefill()
check_prefill.click()

# 确认提交订单
# WebDriverWait(driver, 10, 0.1).until(
# EC.element_to_be_clickable((By.XPATH, '//div[@class="dzp-confirm"]/div/div[3]/a[2]'))
# )
# driver.find_element(By.XPATH, '//div[@class="dzp-confirm"]/div/div[3]/a[2]').click()

train_list = driver.find_elements(By.XPATH, '//*[@id="trainList"]/div')
for item in train_list:
    train_number = item.find_element(By.XPATH, './div[1]/div[1]/div[1]').text
    if train_num == train_number:
        item.find_element(By.XPATH, '//*[@id="trainList"]/div[2]/div[2]/div/div[5]/div[4]/a').click()


